package com.laikan.reader.core.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.laikan.library.managers.ToastMgr;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.core.bean.RankingCategoryBean;
import com.socks.library.KLog;

import java.util.ArrayList;

/**
 * 分类排行适配器
 * Created by gwt on 2016/10/20.
 */
public class RankingCategoryAdapter extends LaikanBaseAdapter<RankingCategoryBean.DataBean> {

    private ViewHolder holder ;
    private boolean isShowNumber = true;

    public RankingCategoryAdapter(Context context , ArrayList<RankingCategoryBean.DataBean> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_ranking_category, null);
            holder.iv_book_icon = (SimpleDraweeView)convertView.findViewById(R.id.iv_book_icon);
            holder.tv_book_category = (TextView)convertView.findViewById(R.id.tv_book_category);
            holder.tv_rank_number = (TextView)convertView.findViewById(R.id.tv_rank_number);
            holder.tv_book_name = (TextView)convertView.findViewById(R.id.tv_book_name);
            holder.tv_book_writer = (TextView)convertView.findViewById(R.id.tv_book_writer);
            holder.tv_book_des = (TextView)convertView.findViewById(R.id.tv_book_des);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        RankingCategoryBean.DataBean dataBean = datas.get(position);

        if(!isShowNumber){
            holder.tv_rank_number.setVisibility(View.GONE);
        }

        if(position >= 99){
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)holder.tv_rank_number.getLayoutParams();
            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            layoutParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
            holder.tv_rank_number.setLayoutParams(layoutParams);
        }

        holder.tv_rank_number.setText(position+1+"");
        holder.iv_book_icon.setImageURI(Uri.parse(dataBean.getIcon())); // 加载图片
        holder.tv_book_name.setText(dataBean.getName());
//        holder.tv_book_category.setText(dataBean.getName()+" | "); //类别
        holder.tv_book_writer.setText(dataBean.getAuthorName());
        holder.tv_book_des.setText(dataBean.getRecommend());

        return convertView;
    }

    //设置排序序号不显示
    public void setNumberGone() {
        isShowNumber = false;
    }

    private class ViewHolder {
        private TextView tv_rank_number,tv_book_name,tv_book_category,tv_book_writer,tv_book_des;
        private SimpleDraweeView iv_book_icon;
    }

}
